/**
 * Created by steven on 2016/4/22.
 */
$(document).on('click', '.trail_li', function() {
    var anjianhao=$(this).find('span.anjianhao').text();
    $('#sel_ah').val(anjianhao);
    $('#dialog1').show();
});



$(document).on('click', '#cancel', function() {
    $('#sel_ah').val('');
    $('#dialog1').hide();
});

$(document).on('click', '#search_trail', function() {
    $('#dialog1').hide();
    var CXMM= $('#xxmm').val();
    $('#CXMM').val(CXMM);
    var AH= $('#sel_ah').html();
    $('#search_form').submit();
});

var total=$('#totalcount').html();
var pages=get_pages(total,15);
var curpage=2;
var flag=true;
window.onscroll = function () {

    //监听事件内容

    if(getScrollHeight() >= getWindowHeight() + getDocumentTop()-40){
        //当滚动条到底时,这里是触发内容
        //异步请求数据,局部刷新dom
        //ajax_function()
        if(!flag){
            return;
        }
        flag=false;
        if(!(curpage<pages+1)){
            flag=true;
            return;
        }
        $('#loadingInfo').show();
        loadMoreData();
    }
}

function get_pages(total,pagesize){
    var page=total%pagesize==0 ? total/pagesize : Math.ceil(total/pagesize);
    return page;
}

//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}

//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;    return scrollHeight;
}

function loadMoreData(){

    var AH_NH=$('select[name="AH_NH"]').val();
    var AH_ZH=$('input[name="AH_ZH"]').val();
    var AH_BH=$('input[name="AH_BH"]').val();
    var DSR=$('input[name="DSR"]').val();
    var url='index.php/TrailProcess/traillist';
    var dt={
        PageNo:curpage,
        PageSize:15,
        AH_NH:AH_NH,
        AH_ZH:AH_ZH,
        AH_BH:AH_BH,
        isAjax:true
    };
    var aj = $.ajax( {
        url:url,// 跳转到 action
        data:dt,
        type:'post',
        cache:false,
        dataType:'json',
        success:function(data) {
            var isSuccess=data.isSuccess;
            if(!isSuccess){
                giveAlert(data.errorMessage);
            }else{
                renderhtml(data.data.informationmodels);
                $('#loadingInfo').hide();
                curpage+=1;
                flag=true;
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            //alert(XMLHttpRequest.status);
            //alert(XMLHttpRequest.readyState);
            //alert(textStatus);
            $('#loadingInfo').hide();
            curpage+=1;
            flag=true;
        }
    });
}

function renderhtml(obj){
    var str='';
    for(var i=0;i<obj.length;i++){
        str+='<li class="list-group-item trail_li">';
        str+='<div  style="margin-bottom: 10px;">';
        str+='<div class="row" >';
        str+='<div class="col-xs-3">';
        str+='<span>案件号</span>';
        str+='</div>';
        str+='<div class="col-xs-9">';
        str+='<span class="anjianhao">'+obj[i].AH+'</span>';
        str+='</div>';
        str+='</div>';
        str+='<div class="row">';
        str+='<div class="col-xs-3">';
        str+='<span>法院</span>';
        str+='</div>';
        str+='<div class="col-xs-9">';
        str+='<span>'+obj[i].CourtName+'</span>';
        str+='</div>';
        str+='</div>';
        str+='<div class="row">';
        str+='<div class="col-xs-3">';
        str+='<span>立案时间</span>';
        str+='</div>';
        str+='<div class="col-xs-9">';
        str+='<span>'+obj[i].LARQ+'</span>';
        str+='</div>';
        str+='</div>';
        str+='<div class="row">';
        str+='<div class="col-xs-3">';
        str+='<span>案件状态</span>';
        str+='</div>';
        str+='<div class="col-xs-9">';
        str+='<span>'+obj[i].AJZT+'</span>';
        str+='</div>';
        str+='</div>';
        str+='<div style="display: none" class="trailId">'+obj[i].Id +'</div>';
        str+='<div class="row">';
        str+='<div class="col-xs-3">';
        str+='<span>当事人</span>';
        str+='</div>';
        str+='<div class="col-xs-9">';
        str+='<div class="col-xs-11" style="padding-left: 0px">';
        for(var j=0;j<obj[i].DSRModels.length;j++){
            str=str+obj[i].DSRModels[j].SSDW+':'+obj[i].DSRModels[j].MC+'</br>';
        }
        str+='</div>';
        str+='<div style="text-align:center;">';
        str+='<span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span>';
        str+='</div>';
        str+='</div>';
        str+='</div>';
        str+='</div>';
        str+='</li>';
    }
    $('ul.list-group').append(str);
}